package com.tiger.novelbld.base;

import net.youmi.android.AdManager;
import net.youmi.android.AdView;

import com.tiger.novelbld.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

public class BaseActivity extends Activity  implements net.youmi.android.AdListener {
	static {
		// 应用Id 应用密码 广告请求间隔(s) 测试模式 应用版本号
		AdManager.init("9da33c3559c40458", "da9ae35488202dab", 20, false, 2.1);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if(this.getClass().getName().contains("MainActivity")){
				confirm("确认退出吗?", "退出");
			}else{
			this.finish();
			}
		}
		return false;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(1, 1, 1, "退出").setIcon(R.drawable.menu_exit);
		menu.add(2, 2, 2, "分享").setIcon(R.drawable.menu_refresh);
		menu.add(3, 3, 3, "关于").setIcon(R.drawable.menu_about);
		return super.onCreateOptionsMenu(menu);
	}

	public void AboutUS() {
		// 关于
		String about_appname = this.getResources().getString(R.string.app_name);
		String about_ver = "1.0版";
		String about_csmail = "tiger.chenlin@gmail.com";
		String about_copyright = "(c)2010-2011\ntiger 保留所有权";
		String app_platform = "Android1.5+";

		alert(this, about_appname + "(" + app_platform + ")" + "\n版本:"
				+ about_ver + "\n客服邮箱:" + about_csmail + "\n版权信息:"
				+ about_copyright + "", "关于");
	}

	public static void alert(Activity act, String msg, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(act);
		builder.setMessage(msg);
		if (title != null)
			builder.setTitle(title);
		builder.setIcon(R.drawable.dialog_information).setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {

			}
		});
		builder.show();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case 1:
			confirm("确认退出吗?", "退出");
			break;
		case 2:
			shareWith(this.getString(R.string.app_name)+"下载地址：");
			break;
		case 3:
			AboutUS();
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void confirm(String msg, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage(msg);
		builder.setTitle(title);
		builder.setIcon(R.drawable.dialog_information).setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				// ActivityManager
				// activityMgr=(ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
				// activityMgr.restartPackage(getPackageName());
				Intent startMain = new Intent(Intent.ACTION_MAIN);
				startMain.addCategory(Intent.CATEGORY_HOME);
				startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(startMain);
				System.exit(0);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
				dialog.dismiss();
			}
		});
		builder.show();
	}

	public void shareWith(String text) {
		// Intent intent=new Intent(Intent.ACTION_SEND);
		// intent.setType("text/plain");
		// intent.putExtra(Intent.EXTRA_SUBJECT, "分享");
		// intent.putExtra(Intent.EXTRA_TEXT,
		// "I would like to share this with you...");
		// startActivity(Intent.createChooser(intent, getTitle()));
		Intent it = new Intent(Intent.ACTION_SEND);
		it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		it.putExtra(Intent.EXTRA_TEXT, text);
		it.setType("text/plain");
		Intent newIntent = Intent.createChooser(it, "请选择");
		newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(newIntent);

	}
	@Override
	public void onConnectFailed(AdView arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReceiveAd(AdView arg0) {
		// TODO Auto-generated method stub
		
	}
}
